package edu.inf3.j2ee.dao;

import edu.inf3.j2ee.crypto.MD5;
import edu.inf3.j2ee.entities.Credentials;
import edu.inf3.j2ee.framework.dbMySQL;

public final class CredentialDao {

	public static Credentials getCredentials (String login, String pwd) throws Exception{
		Credentials cred = null;
		MD5 md5 = MD5.getInstance();
		
		dbMySQL db = new dbMySQL("localhost", "project", "root", "");
		
		db.connect();
		String query = "SELECT user_id,user_group_FK FROM tbCredentials WHERE user_password = \"" 
						+ pwd + "\" AND user_login = \"" + login +"\"";
		//String query = "SELECT user_id,user_group_FK FROM tbCredentials WHERE user_password = \"" 
		//				+ md5.hashData(pwd.getBytes()) + "\" AND user_login = \"" + login +"\"";
		db.execQuery(query);
		
		if (db.nextRecord()){
			cred = new Credentials();
			
			cred.setId(db.getInt("user_id"));
			cred.setLogin(login);
			cred.setPassword(pwd);
			
			int gid = db.getInt("user_group_FK");
			
			cred.setGroup(GroupDao.getGroupById(gid));
			
		}
		
		return cred;
	}
	
}
